//
//  DropListView.m
//  UILbrary
//
//  Created by mac on 16/7/15.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "DropListView.h"
static NSTimeInterval const kListTableViewDropTime = 0.25;
static NSTimeInterval const kListTableViewDismissTime = 0.25;
static CGFloat const topMargn = 15.0;
static CGFloat const lenghToCentWith = 10;
#define SXRGB16Color(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface DropListView()<UITableViewDelegate,UITableViewDataSource>
{
    CGFloat animalBeginOrignX;
    CGFloat animalBeginOrignY;
    CGFloat animalBeginWith;
    CGFloat listViewHight;
}
@property (nonatomic,strong) UIView *listBgView;
@property (nonatomic,strong) UITableView *listTableView;

@end
@implementation DropListView
- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:[UIScreen mainScreen].bounds]){
        self.backgroundColor = [UIColor clearColor];

        animalBeginWith   = frame.size.width;
        animalBeginOrignX = frame.origin.x;
        animalBeginOrignY = frame.origin.y + topMargn;
        listViewHight     = frame.size.height;

        self.listBgView   = [[UIView alloc] initWithFrame:frame];
        self.listBgView.backgroundColor = SXRGB16Color(0xDCDCDC);
        [self addSubview:self.listBgView];
        [self.listBgView addSubview:self.listTableView];
        self.listTableView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)dealloc{
    //
}
- (void)drawRect:(CGRect)rect{
    
    //画三角箭头
    CGFloat pointBeginX = animalBeginOrignX + animalBeginWith/2.0 - lenghToCentWith;
    CGFloat endX = animalBeginOrignX + animalBeginWith/2.0 + lenghToCentWith;
    CGPoint point = CGPointMake(animalBeginOrignX + animalBeginWith/2.0, animalBeginOrignY - topMargn);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(context, pointBeginX, animalBeginOrignY);
    CGContextAddLineToPoint(context, point.x, point.y);
    CGContextAddLineToPoint(context, endX, animalBeginOrignY);
    
    
    //如果加上圆角，就不会有动画效果，额
//    CGFloat radius = 6.0;
//    CGContextAddArcToPoint(context, animalBeginOrignX, animalBeginOrignY - 5, animalBeginOrignX - 5, animalBeginOrignY + listViewHight + 5, radius);
//    
//    CGContextAddArcToPoint(context, animalBeginOrignX - 5, animalBeginOrignY + listViewHight + 5, animalBeginOrignX + animalBeginWith + 5, animalBeginOrignY + listViewHight + 5, radius);
//    CGContextAddArcToPoint(context, animalBeginOrignX + animalBeginWith + 5, animalBeginOrignY + listViewHight + 5, animalBeginOrignX + animalBeginWith + 5, animalBeginOrignY -5, radius);
//    CGContextAddArcToPoint(context, animalBeginOrignX + animalBeginWith + 5, animalBeginOrignY - 5, endX, animalBeginOrignY - 5, radius);
    
    
    CGContextClosePath(context);
    [self.listBgView.backgroundColor setFill];
    [self.listBgView.backgroundColor setStroke];
    CGContextDrawPath(context, kCGPathFillStroke);
}

- (void)showDropListView{
    
    self.listBgView.frame = CGRectMake(animalBeginOrignX, animalBeginOrignY, animalBeginWith, 0);
    self.listTableView.frame = self.listBgView.bounds;
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:kListTableViewDropTime animations:^{
        
        self.listBgView.frame = CGRectMake(animalBeginOrignX, animalBeginOrignY, animalBeginWith, listViewHight);
        self.listTableView.frame = self.listBgView.bounds;//CGRectMake(animalBeginOrignX, animalBeginOrignY, animalBeginWith, 300);
    } completion:^(BOOL finished) {
        for (UIView *view in [self.listBgView subviews]) {
            view.hidden = NO;
        }
    }];
}

- (void)dismissDropListView{

    [UIView animateWithDuration:kListTableViewDismissTime animations:^{
        self.listBgView.frame = CGRectMake(animalBeginOrignX, animalBeginOrignY, animalBeginWith, 0);
        self.listTableView.frame = self.listBgView.bounds;
    } completion:^(BOOL finished) {
        for (UIView *view in [self.listBgView subviews]) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (![touches.anyObject.view isEqual:self.listBgView]) {
        [self dismissDropListView];
    }
}

#pragma mark tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.dropListTableCellHeigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //这里的cell 可以自定义
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dropListCell"];
    cell.textLabel.text = _titleArr[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.tabClickBlock) {
        self.tabClickBlock(cell.textLabel.text,indexPath);
        [self dismissDropListView];
    }
//    NSLog(@"%s",__FUNCTION__);

}





- (UITableView *)listTableView{
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:self.listBgView.bounds style:UITableViewStylePlain];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [_listTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"dropListCell"];
    }
    return _listTableView;
}
@end
